package com.groupfour.attendancemanagementsystem.controller;

import com.groupfour.attendancemanagementsystem.entity.DTO.CRUStudentDTO;
import com.groupfour.attendancemanagementsystem.entity.VO.RStudentVO;
import com.groupfour.attendancemanagementsystem.service.StudentService;
import com.groupfour.attendancemanagementsystem.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class StudentManagementController {

    @Autowired
    private StudentService studentService;


    /**
     * @author 扶永超
     * @param cruStudentDTO
     * @describe 添加学生信息
     */
    @RequestMapping("/doAddStudent")
    public Result<String> doAddStudent(CRUStudentDTO cruStudentDTO){
        int result = studentService.addStudent(cruStudentDTO);
        if(result>0){
            return Result.ok("添加");
        }else{
            return Result.fail("添加");
        }
    }

    /**
     * @author 扶永超
     * @param studentCode
     * @describe 删除学生信息（逻辑删）
     */
    @RequestMapping("/doDeleteStudent/{studentCode}")
    public Result<String> doDeleteStudent(@PathVariable("studentCode")String studentCode){
        int result = studentService.deleteStudent(studentCode);
        if(result>0){
            return Result.ok("删除");
        }else{
            return Result.fail("删除");
        }
    }

    /**
     * @author 扶永超
     * @param cruStudentDTO
     * @param studentCode
     * @describe 修改学生信息
     */
    @PostMapping("/doUpdateStudent/{studentCode}")
    public Result<String> doUpdateStudent(CRUStudentDTO cruStudentDTO,@PathVariable("studentCode")String studentCode){
        int result = studentService.updateStudent(cruStudentDTO,studentCode);
        if(result>0){
            return Result.ok("修改");
        }else{
            return Result.fail("修改");
        }
    }

    /**
     * @author 扶永超
     * @param cruStudentDTO
     * @describe 查询指定班级学生
     */
    @PostMapping("/doSelectStudentByClassesCode/{pageNum}/{pageSize}")
    public Result doSelectStudentByClassesCode(
            @PathVariable Integer pageNum,
            @PathVariable Integer pageSize,
            @RequestBody CRUStudentDTO cruStudentDTO) {
        Map<String,Object> studentMap = studentService.selectStudent(cruStudentDTO,pageNum,pageSize);
        if((Integer)studentMap.get("pageCount") > 0){
           return Result.ok(studentMap);
        }else{
            return Result.fail();
        }
    }


}
